<?php


namespace app\admin\controller\system;

use app\admin\model\SystemAdmin;
use app\admincommon\controller\AdminController;
use think\facade\App;
use think\facade\View;

/**
 * Class Admin
 * @package app\admin\controller\system
 * @ControllerAnnotation(title="管理员管理")
 */

class Admin extends AdminController
{

    protected $admin_list;

    public function __construct()
    {
        $this->model = new SystemAdmin();

        $this->admin_list=$this->model::order("sort desc,id desc")->select()->toArray();

    }

    function index(){

        View::assign([
            "admin_list"=>$this->admin_list
        ]);

        return View::fetch();
    }


    function add(){

        if(request()->isPost()){

            $post = request()->post();

            try {

                $save = $this->model->save($post);

            } catch (\Exception $e) {

                $this->error('保存失败：用户已经存在');

            }

            $save ? $this->success('保存成功') : $this->error('保存失败');

        }

        View::assign([
            "auth_list"=>$this->auth_list
        ]);

        return View::fetch();
    }



    function edit(){

        $id = input("id");
        $admin = $this->model->find($id);

        empty($admin) && $this->error("用户不存在");

        if(request()->isPost()){
            $post = request()->post();
            try {
                $save = $this->model->update($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }

        View::assign([
            "admin" => $admin,
            "auth_list"=> $this->auth_list
        ]);

        return View::fetch();
    }




    //修改密码

    function password(){

        if(request()->isPost()){

            $password=input('password');
            $newpass=input('newpass');

            $admin = session('admin');
            $user = SystemAdmin::where(['username' => $admin['username']])->find();

            if($user->password == password($password)){
                $res = SystemAdmin::where("id",$user->id)->update(["password"=>password($newpass)]);
                if($res){
                    session('admin',null);
                    return $this->success('重置成功，请重新登录',[],__url('/admin/login/index'));
                }
            }else{
                return $this->result([],0,"原来密码有误");
            }

        }

        return View::fetch();
    }

}









?>